<template>
    <view>
		<u-navbar
		:autoBack="true"
		:leftIconColor="tksetting.titlecolor" :bgColor="tksetting.color" :titleStyle="{color:tksetting.titlecolor}" :title="title"  :placeholder="true">
		</u-navbar>
        <!-- color：主题色 addressList:地址管理数据 @chooseClick：选择事件 @editClick：编辑事件  -->
		<view class="empty_info" v-if="!addressList">
			你还没有添加地址
		</view>
        <CcAddressSet v-else :colors="tksetting.color" :addressList="addressList" @delClick='delClick' @editClick='editClick'  @chooseClick="chooseClick" ></CcAddressSet>
        <view class="save">
            <view class="btn" :style="'background:' + tksetting.color" @tap="addAddress">添加收货地址</view>
        </view>

    </view>
</template>

<script>
	import { mapState } from 'vuex';
	import CcAddressSet from './components/cc-addressSet/cc-addressSet'
	import {useRouter} from '@/utils/utils.js'
    export default {
        data() {
            return {
				title:'地址管理',
                colors: '',
                addressList: [],
				types:1 //types为1,则直接从个人中心进入,否则为购买商品点击进入
            };
        },
        props: {},
        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function(opt) {
			if(opt.data){
				const params = JSON.parse(decodeURIComponent(opt.data));
				this.types=params.types;
			}
			this.init();
			//uni.$on("refresh",this.refresh)
        },
		onUnload() {
			//uni.$off(("refresh",this.refresh))
		},
		computed: {
			...mapState(['tksetting'])
		},
		components:{
		  CcAddressSet
		},	
        methods: {
			refresh(val){
				console.log(val)
				//this.addressList.push(val)
				//this.init()
			},
			async init(){
				   
					const that = this;
					//const utils = that.utils;
					const {code,data}= await that.$api.addlist();
					 console.log(data)
					this.addressList=data;
				},
            // 编辑地址
            editClick(item) {
                // uni.showModal({
                //     title: '点击编辑条目',
                //     content: '点击编辑条目' + JSON.stringify(item)
                // })
				this.$api.adddefault({id:item.id}).then(res=>{
					
					this.init()
					
				})
				
            },
			delClick(item) {
				let that = this;
				uni.showModal({
					title: '系统提示',
					content: '你确定要删除吗?',
					success(res) {
						if (res.confirm) {
							 that.$api.del_useradd({id:item.id});
							 that.init();
						}
					}
				});
			   
			},
            chooseClick(item){
				console.log(item)
				if(this.types!=1){
					// let pages = getCurrentPages();
					// let prevPage = pages[pages.length - 2];
					// prevPage.$vm.pro.adds=item.address+item.moreAddres
					// prevPage.$vm.add_id=item.id
					uni.$emit('upadds',{add_id:item.id,name:item.name,phone:item.phone,adds:item.address+item.moreAddres});
					uni.navigateBack();
				}
            },
            // 增加地址
            addAddress() {
				useRouter('/pages/user/addlist/addform')
            },
        }
    };
</script>
<style lang="scss" scoped>
    .save {
        position: fixed;
        bottom: 0;
        width: 100%;
        height: 120upx;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .save view {
        display: flex;
    }

    .save .btn {
        box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.4);
        width: 70%;
        height: 80upx;
        border-radius: 80upx;
        background-color: #f23a3a;
        color: #fff;
        justify-content: center;
        align-items: center;
        font-size: 30upx;
        margin-bottom: 60px;
    }
	.empty_info{ text-align: center; padding: 20px; color: #eaeaea;}
</style>